<template>
<div  class="bc">
  <el-row >
  </el-row>
  <div  style="margin: 10px 0">
    <el-input v-model="search" placeholder="请输入文章标题或账号名称" style="width: 30%" clearable @clear="clear"></el-input>  <el-button @click="SearchList">查询</el-button>
  </div>

  <el-table
      :data="articleList"
      border
      style="width: 100%"
      stripe
  >
      <el-table-column  type="expand" label="评论"  width="50px">
          <template #default="scope">
              <Comemt :topId="scope.row.id"></Comemt>
        </template>
    </el-table-column>
    <el-table-column
        prop="title"
        label="标题"
        >
    </el-table-column>
    <el-table-column
        prop="alias"
        label="昵称"
        >
    </el-table-column>
    <el-table-column
        prop="username"
        label="账号名称"
        >
    </el-table-column>
    <el-table-column
        prop="view"
         width="100px"
        label="浏览量"
        >
          <!-- sortable -->
    </el-table-column>
    <el-table-column
        prop="createTime"
        label="发布时间"
        width="200px"
        :formatter="formatDate"
        >
    </el-table-column>
        <el-table-column
        label="操作">
        <template #default="scope">
           <router-link :to="{name:'post-detail',params:{id:scope.row.id}}" target="_blank">
                       <el-button type="primary" size="small" @click="handleEdit(scope.row.id)">前往查看</el-button>
                      </router-link>
                       <el-button type="danger" size="small" @click="handledelete(scope.row.id)">删除</el-button>

        </template>

    </el-table-column>
  </el-table>

  <div>
    <div>
      <el-pagination
        :currentPage="current"
        :page-sizes="[3, 5, 10, 20]"
        :page-size="size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    >
    </el-pagination>

    </div>
  </div>
</div>
</template>

<script>
import { deleteTopic, getSearchList } from '@/api/post'

import Comemt from './T-View/Com.vue'
export default {
  components: {
    Comemt
  },
  data () {
    return {
      search: '',
      searchUser: '',
      dialogVisible: false,
      articleList: [],
      topicid: '',
      // page: {

      // }
      current: 1,
      size: 10,
      total: 0,
      // query: {
      //   keyword: this.search,
      //   pageNum: 1,
      //   pageSize: 10,
      //   total: 0
      // }
      formatDate (row, column) {
      // 获取单元格数据
        const data = row[column.property]
        if (data == null) {
          return null
        }
        const dt = new Date(data)
        return dt.getFullYear() + '-' + (dt.getMonth() + 1) + '-' + dt.getDate() + ' ' + dt.getHours() + ':' + dt.getMinutes() + ':' + dt.getSeconds()
      }
    }
  },
  created () {
    this.init()
  },
  methods: {
    init () {
      getSearchList(this.current, this.size, this.search).then((response) => {
        const { data } = response
        this.current = data.current
        this.total = data.total
        this.size = data.size
        this.articleList = data.records
      })
      console.log(this.articleList)
    },
    SearchList () {
      getSearchList(1, this.size, this.search).then((response) => {
        const { data } = response
        this.current = data.current
        this.total = data.total
        this.size = data.size
        this.articleList = data.records
      })
    },
    handleSizeChange (size) {
      this.size = size
      this.init()
    },
    handleCurrentChange (pageNo) {
      this.current = pageNo
      this.init()
    },
    handleEdit (row) {
      console.log(row)
    },

    // handledelete (id) {
    //   // console.log(id)
    //   deleteTopic(id).then(value => {
    //     const { code, message } = value
    //     alert(message)
    //     if (code === 200) {
    //       setTimeout(() => {
    //         this.$router.push({ path: '/' })
    //       }, 500)
    //     }
    //   })
    // },

    handledelete (id) {
      this.$confirm('是否要删除这篇文章?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log(id)
        try {
          deleteTopic(id).then(value => {
            this.$message.success('删除成功')
            // this.fetchParentComments(this.comment.id)
            this.init()
            // alert(123)
          })
        } catch (e) {
          this.$message.error('删除失败')
        }
      })
        .catch(() => {
          // return true
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    clear () {
      this.current = 1
      this.init()
    }

  },
  mounted () {}
}
</script>

<style lang="less" scoped>
.bc{
  padding: 10px;
  background-color: #fff;
  height: 100vh;
}
.el-table{
  // border: 3px solid rgb(177, 177, 177);
}
</style>
